package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.hongsheng.pojo.*;
import com.hongsheng.pojo.Package;
import com.hongsheng.service.*;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author admin
 * @version 1.0
 * @date 2023/8/10 18:47
 */
@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private DepositoryService depositoryService;
    @Resource
    private DriverService driverService;
    @Resource
    private PackgeService packageService;
    @GetMapping()
    public R findAll(){
        List<Goods> all = goodsService.findAll();
        for (Goods goods : all) {
            System.out.println(goods);
        }
        return new R(true,all);
    }
    @GetMapping("/oid")
    public R getAllOrderId(){
        List<Integer> allOrderId = goodsService.getAllOrderId();
        return new R(true,allOrderId);
    }
    @GetMapping("/depository")
    public R getAllDepository() {
        List<Depository> allDeps = depositoryService.findAllDepository();
        return new R(true,allDeps);
    }
    @GetMapping("/driver")
    public R getAllDriver() {
        List<Driver> driverList = driverService.findFreeDriver();
        return new R(true,driverList);
    }
    @GetMapping("/package")
    public R getAllPackage() {
        List<Package> list = packageService.list();
        return new R(true,list);
    }
    @PostMapping
    public R insert(@RequestBody Goods goods){
        Driver driver = goods.getDriver();
        driver.setStatus(1);
        Boolean aBoolean = driverService.updateDriver(driver);
        Boolean flag = goodsService.insertGoods(goods);
        return new R(flag&&aBoolean);
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        Boolean aBoolean = goodsService.deleteGoods(id);
        return new R(aBoolean);
    }

    @GetMapping("/{id}")
    public R findGoodsById(@PathVariable Integer id){
        return new R(true,goodsService.findGoodsById(id));
    }
    //编辑货物
    @PutMapping
    public R updateGoods(@RequestBody Goods goods){
        Boolean flag = goodsService.updateGoods(goods);
        return new R(flag);
    }
    @GetMapping("/getOrderStatus/{id}")
    public R getOrderStatusById(@PathVariable Integer id){
        return new R(true,goodsService.getOrderStatusById(id));
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public R getAll(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Goods goods){
        PageInfo<Goods> pageGoods = goodsService.getPageGoods(currentPage, pageSize, goods);
        return new R(true,pageGoods);
    }
    @GetMapping("/getCost/{id}")
    public R getCost(@PathVariable Integer id){
        Double cost = goodsService.getCost(id);
        return new R(true,cost);
    }
    @GetMapping("/getIncome/{id}")
    public R getIncome(@PathVariable Integer id){
        Double income = goodsService.getIncome(id);
        return new R(true,income);
    }

    @PostMapping("/goodsCost")
    public R getGoodsCost(@RequestBody Bill bill){
        Bill bill1=new Bill();
        bill1.setAmount(bill.getAmount());
        bill1.setBillDesc(bill.getBillDesc());
        Boolean aBoolean = goodsService.GoodsCost(bill1);
        return new R(true,aBoolean);
    }

    @PostMapping("/goodsIncome")
    public R getGoodsIncome(@RequestBody Bill bill){
        Bill bill1=new Bill();
        bill1.setAmount(bill.getAmount());
        bill1.setBillDesc(bill.getBillDesc());
        Boolean aBoolean = goodsService.GoodsIncome(bill1);
        return new R(true,aBoolean);
    }
    /**
     * 查询类型订单数量
     * @return
     */
    @GetMapping("/selectGoodsByQuantity")
    public R selectGoodsByQuantity(){
        return new R(true,goodsService.selectGoodsByQuantity());
    }


//    @GetMapping("/getDriverStatus/{id}")
//    public R getDriverStatusById(@PathVariable Integer id){
//        return new R(true,goodsService.getDriverStatusById(id));
//    }


//    @GetMapping("/depName")
//    public R getAllDepositoryName(){
//        List<String> allDepositoryName = goodsService.getAllDepositoryName();
//        return new R(true,allDepositoryName);
//    }
//    @GetMapping("/driName")
//    public R getAllDriverName(){
//        List<String> allDriverName = goodsService.getAllDriverName();
//        return new R(true,allDriverName);
//    }
}
